<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px" :inline="true">
            <el-form-item label="视频标题：">
              <el-input v-model="tableFrom.keyword" @keyup.enter.native="getList(1)" placeholder="请输入视频标题"
                class="selWidth" size="small">
                <el-button slot="append" icon="el-icon-search" size="small" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to="{ path: roterPre + '/busines/video/addVideo' }">
          <el-button size="small" type="primary">添加视频</el-button>
        </router-link>
      </div>
      <el-table :data="tableData.data" style="width: 100%" size="small">
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column label="视频封面" min-width="80" prop="image">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image style="width: 36px; height: 36px" :src="scope.row.image_src"
                :preview-src-list="[scope.row.image_src]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="视频标题" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="视频链接" min-width="180">
          <template slot-scope="scope">
            <a target="_blank" :href="scope.row.video_src">{{ scope.row.video_src }}</a>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="150">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | tiem }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否置顶" min-width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" @change="onchangeIsShow(scope.row)" />
          </template>
        </el-table-column> -->
        <el-table-column label="是否置顶" min-width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.hot" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否"
              @change="onchangeIsShow('hot', scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="视频状态" min-width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="显示"
              inactive-text="不显示" @change="onchangeIsShow('status', scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="关联商品" prop="loves" :formatter="typeName">
        </el-table-column>
        <el-table-column label="点赞数量" prop="loves">
        </el-table-column>
        <el-table-column label="评论数量" prop="comments">
        </el-table-column>
        <el-table-column label="转发数量" prop="forwards">
        </el-table-column>
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{ path: roterPre + '/busines/video/addVideo?id=' + scope.row.id }">
              <el-button type="text" size="small" class="mr10">编辑</el-button>
            </router-link>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  getVideoList,
  delVideo,
  deitVideoStatus
} from "@/api/buss"
import { roterPre } from '@/settings'
export default {
  name: 'Article',
  data() {
    return {
      roterPre: roterPre,
      tableFrom: {
        page: 1,
        limit: 20,
        keyword: ''
      },
      tableData: {
        data: [],
        total: 0
      }
    }
  },
  mounted() {
    this.getList()
  },
  filters: {
    // 处理到期时间
    tiem: function (createTime) {
      var date = new Date(createTime * 1000)
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hour = date.getHours();
      var minute = date.getMinutes();
      var second = date.getSeconds();
      return year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒"
    }
  },
  methods: {
    typeName(row) {
      if (row.product_id) {
        return '已关联商品'
      } else {
        return '未关联商品'
      }
    },
    // 获取视频列表
    getList() {
      getVideoList(this.tableFrom).then(res => {
        console.log(res, 'res')
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure().then(() => {
        delVideo(id).then(({ message }) => {
          this.$message.success(message)
          this.getList()
        }).catch(({ message }) => {
          this.$message.error(message)
        })
      })
    },
    // 修改视频状态
    onchangeIsShow(type, row) {
      var data = {
        id: row.id,
        field: type,
        value: row.status
      }
      deitVideoStatus(data).then(({
        message
      }) => {
        this.$message.success(message)
        this.getList()
      }).catch(({
        message
      }) => {
        this.$message.error(message)
      })
    },
  }
}
</script>

<style scoped lang="scss">
.selWidth {
  width: 350px;
}
</style>
